package com.zhhs.galaxyott.adapters;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.movieDetailActivity.MovieDetailActivity;
import com.zhhs.galaxyott.beans.CollectionBean;
import com.zhhs.galaxyott.utils.Logs;

public class CollectGridAdapter extends BaseAdapter implements OnClickListener {

    private Holder holder;
    private Context mContext;
    private List<CollectionBean> mlist;

    public CollectGridAdapter(Context mContext, List<CollectionBean> mlist) {

        this.mContext = mContext;
        this.mlist = mlist;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_collectrecords_item, null);

            holder.poster_img = (ImageView) convertView.findViewById(R.id.poster);
            holder.poster_name = (TextView) convertView.findViewById(R.id.poster_name);

            convertView.setTag(holder);

        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.poster_img.setTag(R.id.poster, mlist.get(position).getPid());
        holder.poster_name.setText(mlist.get(position).getName());
        Glide.with(mContext).load(mlist.get(position).getImgurl()).placeholder(R.drawable.movie_item_default)
                .crossFade().into(holder.poster_img);
        // holder.poster_img.setTag(position);
        holder.poster_img.setOnClickListener(this);
        Logs.logD("DEBUG_LC", "THIS IS onKeyDown-->0");

        holder.poster_img.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Logs.logD("DEBUG_lc", "THIS IS onFocusChange");
                    ((TextView) ((LinearLayout) v.getParent()).getChildAt(1)).setSelected(true);
                } else {
                    ((TextView) ((LinearLayout) v.getParent()).getChildAt(1)).setSelected(false);
                }

            }
        });

        return convertView;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(mContext, MovieDetailActivity.class);
        Logs.logD("DEBUG_AD", "THIS IS -->POSITION:" + v.getTag());
        intent.putExtra("movieid", (Integer) v.getTag(R.id.poster));
        mContext.startActivity(intent);

    }

    class Holder {
        ImageView poster_img;
        TextView poster_name;
    }

}
